package com.easylinkin.linkappapi.operatelog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.operatelog.entity.OperateLog;
import com.easylinkin.linkappapi.operatelog.mapper.OperateLogMapper;
import com.easylinkin.linkappapi.operatelog.service.OperateLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2020-04-12
 */
@Service
public class OperateLogServiceImpl extends ServiceImpl<OperateLogMapper, OperateLog> implements OperateLogService {


    @Override
    public boolean addLog(OperateLog operateLog) {
        return save(operateLog);
    }

    @Override
    public IPage<OperateLog> getPage(Page page, OperateLog operateLog) {
        QueryWrapper<OperateLog> queryWrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(operateLog.getLogId())) {
            queryWrapper.eq("log_id", operateLog.getLogId());
        }
        if (StringUtils.isNotEmpty(operateLog.getModuleName())) {
            queryWrapper.eq("module_name", operateLog.getModuleName());
        }
        if (StringUtils.isNotEmpty(operateLog.getCreator())) {
            queryWrapper.eq("creator", operateLog.getCreator());
        }
        if (null != operateLog.getStatus()) {
            queryWrapper.eq("status", operateLog.getStatus());
        }
        if (StringUtils.isNotEmpty(operateLog.getContent())) {
            queryWrapper.like("content", operateLog.getContent());
        }
        if (StringUtils.isNotEmpty(operateLog.getIp())) {
            queryWrapper.like("ip", operateLog.getIp());
        }
        if (StringUtils.isNotEmpty(operateLog.getUrl())) {
            queryWrapper.like("url", operateLog.getUrl());
        }

        if (StringUtils.isNotEmpty(operateLog.getQueryTimeStart())) {
            queryWrapper.ge("create_time", operateLog.getQueryTimeStart());
        }
        if (StringUtils.isNotEmpty(operateLog.getQueryTimeEnd())) {
            queryWrapper.le("create_time", operateLog.getQueryTimeEnd());
        }
        return baseMapper.selectPage(page, queryWrapper);
    }
}
